<?php 
/* P3(c)

******************************************************************************
                                                                                      
                         __                                                        
                        /__)                                                       
                       /__        P3 - moving Technologie            
                        __)                                                          
                        __)                                                          
                                                                                        
                                                                                        
       Copyright 2009 by p3.co.at                                                                                 
                                                                                        
       visit www.p3.co.at to get more Informations 
       about Teriko - Betriebsdatenerfassung                                                                                 
       - it's not allowed to change this scripts
       - it's not allowed to remove this copyright-tag

       -- you can download the complete license   
          under http://www.teriko.org

 (c) by Patrik Pfaffenbauer and P3 (http://www.p3.co.at)

 | Programname: P3.Teriko
 | Authtor: P3.Verein
 | Coder: Patrik Pfaffenbauer
 | Version: 0.1

 | Change Index
 |_____________________________________________
 |                                             |
 |4.3.2010 Erstellung | Patrik Pfaffenbauer    |
 |_____________________________________________|

******************************************************************************

P3(c) */

class Homenews extends Base
{
	public function Homenews()
	{
		$this->title = $GLOBALS['lang']->GetLanguage("Homenews", "title");
		$this->toolbar = true;
		$this->moduleID = 901;
		$this->permission = GetPermission($this->moduleID);
		$this->toolbarClass = "icon-48-content";
		
		if(PermissionCount($this->permission) == 0)
		{
			$this->allowed = false;
		}
	}
	
	private function CheckInput()
	{
		include("./_tables.php");
		if($_POST['header'] == "")
		{
			$this->errorText = $GLOBALS['lang']->GetLanguage("Panel", "missedMandatoryField");
			return false;
		}
		
		return true;
	}

	public function Add()
	{		
		include("./_tables.php");
		$panel = new Panel();
	
		if($this->error)
		{
			$panel->AddRow();
			$panel->AddText($this->errorText);
		}
		
		$panel->AddRow();
		$panel->AddMandatoryTextBox("header", $GLOBALS['lang']->GetLanguage("Common", "head"), $_POST['header'], 45, 30);
		$panel->AddRow();
		$panel->AddCKEditor("text", $GLOBALS['lang']->GetLanguage("Common", "text"), $_POST['pauseName'], 40, 10);
		$panel->AddRow();
		
		$panel->AddHidden("date", date("Y-m-d H:i:S"));
		$panel->AddHidden("action", "save");
		$panel->AddHidden("option", "pause");
		
		$panel->Close();
	}
	public function Edit()
	{		
		include("./_tables.php");
		
		$res = $GLOBALS['db']->Fetch($GLOBALS['db']->Query("SELECT * FROM ".$tblhomenews." WHERE id=".$_POST['id'].""));
		
		$panel = new Panel();
		
		if(isset($_POST['header']))
		{
			if($this->error)
			{
				$panel->AddRow();
				$panel->AddText($this->errorText);
			}
			
			$res->header = $_POST['header'];
			$res->text = $_POST['text'];
		}

		$panel->AddRow();
		$panel->AddMandatoryTextBox("header", $GLOBALS['lang']->GetLanguage("Common", "head"), htmlentities($res->header), 45, 30);
		$panel->AddRow();
		$panel->AddCKEditor("text", $GLOBALS['lang']->GetLanguage("Common", "text"), $res->text, 40, 10);
		$panel->AddRow();
		
		//$panel->AddHidden("date", date("Y-m-d H:i:S"));
		$panel->AddHidden("action", "update");
		$panel->AddHidden("option", "pause");
		$panel->AddHidden("id", $_POST['id']);
		
		$panel->Close();
	}
	
	public function AnalysPost()
	{
		include("./_tables.php");
		$this->filter = $_POST['search'];
					
		if($_POST['action'] == "add")
		{
			$this->Add();
		}
		else if($_POST['action'] == "edit")
		{
			$this->Edit();
		}
		else if($_POST['action'] == "search")
		{
			$this->Paint();
		}
		else if($_POST['action'] == "paint")
		{
			$this->filter = "";
			$this->Paint();
		}
		else if($_POST['action'] == "save")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->SaveObject($tblhomenews);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Add();
			}
		}
		else if($_POST['action'] == "update")
		{
			if($this->CheckInput())
			{
				$GLOBALS['db']->UpdateObject($tblhomenews);
				$this->Paint();
				$this->error = false;
			}
			else
			{
				$this->error = true;
				$this->Edit();
			}
		}
		else if($_POST['action'] == "delete")
		{
			$GLOBALS['db']->Delete($tblhomenews);
			$this->Paint();
		}
		else
		{
			$this->Paint();
		}
	}

	public function Paint()
	{
		include("./_tables.php");
		
		echo 
		'<div id="cpanel">';
				
		$dgv = new DataGridView($this);
		$dgv->OpenHeader();
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "head"), "17%", true);
		$dgv->AddHeader($GLOBALS['lang']->GetLanguage("Common", "text"), "20%", true);
		$dgv->AddHeader("", "50%", true);
		$dgv->CloseHeader();
		
		$res = $GLOBALS['db']->Query("SELECT * FROM ".$tblhomenews);
		
		while($line = $GLOBALS['db']->Fetch($res))
		{
			$dgv->AddRow($line->id);
			$dgv->AddData($line->header, "", "", "header");
			$dgv->AddData(ShortText(htmlspecialchars($line->text), 100, " ..."), "", "", "text");
			$dgv->AddData("", "", "", "");
			
			$dgv->CloseRow();
		}
		
		$dgv->CloseDataGrid();
		
		echo '</div>';
	}
	public function Toolbar()
	{	
		if(!$this->allowed)
		{
			return;
		}
		
			PaintDefaultToolbar($this);
	}
	
	public function AddCSS()
	{
			
	}
	public function AddJS()
	{
		
	}
}

?>